package main

import (
	"database/sql"
	"fmt"
	//导入mysql驱动
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	//连接数据库
	db, err := sql.Open("mysql", "sc:123456@tcp(192.168.116.129:3306)/sc?charset=utf8")
	//若出现错误直接退出
	if err!=nil {
		fmt.Println(err)
		return
	}

	//定义查询的结构
	type info struct {
		id int `db:"id"`
		name string `db:"name"`
		age string `db:"age"`
		address string `db:"address"`
	}

	//sql查询语句
	rows,err:=db.Query("SELECT * FROM StuInfo")

	//逐行写入结构体，并输出
	for rows.Next(){
		var s info
		err=rows.Scan(&s.id,&s.name,&s.age,&s.address)
		fmt.Println(s)
	}
	rows.Close()
}